/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.feedback.repository.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.common.core.enums.Symbol;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import info.peigen.hotpot.component.feedback.domain.Feedback;
import info.peigen.hotpot.component.feedback.repository.entity.FeedbackEntity;
import info.peigen.hotpot.component.feedback.repository.mapper.FeedbackMapper;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldDefaults;
import org.noear.solon.annotation.Component;


/**
 * <b>(FeedbackRepository)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/11/17
 */
@Data
@EqualsAndHashCode(callSuper = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
@Component
public class FeedbackRepository extends AbstractBeetlSqlRepository<FeedbackMapper, FeedbackEntity> {
    public void store(Feedback feedback) {
        FeedbackEntity entity = BeanUtil.copyProperties(feedback, FeedbackEntity.class, "images");
        entity.setImages(CollUtil.join(feedback.getImages(), Symbol.COMMA.code()));

        saveOrUpdate(entity);
    }

    public Feedback active(Integer id) {
        FeedbackEntity entity = getMapper().unique(id);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!id=" + id));

        return FeedbackConvertor.convert(entity);
    }
}